package com.flynn.rock.crm.controller;

import com.flynn.rock.annotation.OperateLog;
import com.flynn.rock.crm.model.customer.request.PageCustomerRequest;
import com.flynn.rock.crm.model.customer.request.PageFollowUpRecordRequest;
import com.flynn.rock.crm.model.customer.request.PageOperateRecordRequest;
import com.flynn.rock.crm.model.customer.request.SaveCustomerRequest;
import com.flynn.rock.crm.model.customer.request.SaveFollowUpRecordRequest;
import com.flynn.rock.crm.model.customer.request.TransferCustomerRequest;
import com.flynn.rock.crm.model.customer.vo.CustomerVo;
import com.flynn.rock.crm.model.customer.vo.FollowUpRecordIdVo;
import com.flynn.rock.crm.model.customer.vo.ListCustomerVo;
import com.flynn.rock.crm.model.customer.vo.CustomerIdVo;
import com.flynn.rock.crm.model.customer.vo.ListFollowUpRecordVo;
import com.flynn.rock.crm.model.customer.vo.ListOperateRecordVo;
import com.flynn.rock.crm.service.customer.CustomerService;
import com.flynn.rock.enums.BusinessType;
import com.flynn.rock.enums.OperateType;
import com.flynn.rock.model.PageResult;
import com.flynn.rock.model.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/crm/customer")
@Tag(name = "客户管理")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @OperateLog(operateTitle = "新增客户信息", operateType = OperateType.CREATE, businessType = BusinessType.CRM)
    @PostMapping
    @Operation(summary = "新增客户信息")
    public Response<CustomerIdVo> insertCustomer(@RequestBody @Validated SaveCustomerRequest request) {
        return Response.success(customerService.saveCustomer(request));
    }

    @OperateLog(operateTitle = "更新客户信息", operateType = OperateType.UPDATE, businessType = BusinessType.CRM)
    @PutMapping
    @Operation(summary = "更新客户信息")
    public Response<CustomerIdVo> updateCustomer(@RequestBody @Validated SaveCustomerRequest request) {
        return Response.success(customerService.saveCustomer(request));
    }

    @GetMapping("/{customerId}")
    @Operation(summary = "获取客户详情")
    public Response<CustomerVo> getCustomer(@PathVariable("customerId") Long customerId) {
        return Response.success(customerService.getCustomer(customerId));
    }

    @OperateLog(operateTitle = "删除客户", operateType = OperateType.DELETE, businessType = BusinessType.CRM)
    @DeleteMapping("/{customerId}")
    @Operation(summary = "删除客户")
    public Response<Void> deleteCustomer(@PathVariable("customerId") Long customerId) {
        customerService.deleteCustomer(customerId);
        return Response.success(null);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询客户列表")
    public Response<PageResult<ListCustomerVo>> pageCustomer(@RequestBody PageCustomerRequest request) {
        return Response.success(customerService.pageCustomer(request));
    }

    @OperateLog(operateTitle = "客户转派", businessType = BusinessType.CRM, operateType = OperateType.TRANSFER)
    @PutMapping("/transfer")
    @Operation(summary = "客户转派")
    public Response<Void> transferCustomer(@Parameter @RequestBody @Validated TransferCustomerRequest request) {
        customerService.transferCustomer(request);
        return Response.success(null);
    }

    @OperateLog(operateTitle = "保存跟进记录", operateType = OperateType.CREATE, businessType = BusinessType.CRM)
    @PostMapping("/followUpRecord")
    @Operation(summary = "保存跟进记录")
    public Response<FollowUpRecordIdVo> saveFollowUpRecord(@RequestBody @Validated SaveFollowUpRecordRequest request) {
        return Response.success(customerService.saveFollowUpRecord(request));
    }


    @PostMapping("/followUpRecord/page")
    @Operation(summary = "分页查询跟进记录")
    public Response<PageResult<ListFollowUpRecordVo>> pageFollowUpRecord(@RequestBody @Validated PageFollowUpRecordRequest request) {
        return Response.success(customerService.pageFollowUpRecord(request));
    }

    @PostMapping("/operateRecord/page")
    @Operation(summary = "分页查询客户操作记录")
    public Response<PageResult<ListOperateRecordVo>> pageOperateRecord(@RequestBody @Validated PageOperateRecordRequest request) {
        return Response.success(customerService.pageOperateRecord(request));
    }
}
